﻿using Microsoft.EntityFrameworkCore;
using RuanGong.background.A.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Table_pet.background.Classes;

namespace RuanGong.background.A
{
    internal class LocalHealthyDataBaseAPI
    {
        private static readonly DbContextOptions<Hcontext> contextOptions = new DbContextOptionsBuilder<Hcontext>()
               .UseMySQL("Server=localhost;Database=HDB;User=root;Password=zamm030130")
               .Options;
        public static List<Local_Healthy_DataBase> readAll()
        {
            using (var context = new Hcontext(contextOptions))
            {
                var q = context.local_Healthy_DataBases;
                return q.ToList();
            };
        }

        public static int add(Local_Healthy_DataBase Local_Healthy_DataBase)
        {
            using (var context = new Hcontext(contextOptions))
            {
                context.local_Healthy_DataBases.Add(Local_Healthy_DataBase);
                context.SaveChanges();
                return Local_Healthy_DataBase.Id;
            }
        }

        public static Local_Healthy_DataBase deleteById(int id)
        {
            using (var context = new Hcontext(contextOptions))
            {
                var heal = context.local_Healthy_DataBases.FirstOrDefault(h => h.Id == id);
                if (heal != null)
                {
                    context.local_Healthy_DataBases.Remove(heal);
                    context.SaveChanges();
                }
                return heal;
            }
        }

        public static Local_Healthy_DataBase searchById(int id)
        {
            using (var context = new Hcontext(contextOptions))
            {
                var heal = context.local_Healthy_DataBases.FirstOrDefault(h => h.Id == id);
                return heal;
            }
        }
    }
}
